﻿<?xml version="1.0" encoding="utf-8"?>
<component>
    <?component error="false" debug="false"?>
    
    <registration>
      <public name="version" />
      <property name="connectionString" />
      <property name="pageCount" />
      <property name="path" />
      <property name="debug" />
      <property name="execute">
          <method name="sql" name="relative" />
    <parameter name="field" name="size" />
    </method>
      <method type="ASP" id="ASP"/>
    <parameter language="JScript" runat="server">
    <parameter>    	
        var version = '1.00';
        var connectionString = '';
        var path = '';
        var debug = false;
        var pageCount = 0;
        
        var _connection = Server.CreateObject('ADODB.Connection');
        var _recordset = Server.CreateObject('ADODB.Recordset');
        
        var _opened = false;
        var _recordCount = 0;
        
        function _open() {
            try {
                if (connectionString) {
                    _connection.ConnectionString = connectionString;
                }
                else {
                    _connection.ConnectionString = 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=' + Server.MapPath(path);
                }
                
                _connection.Open();
                _opened = true;
            }
            catch (_error) {
                Response.Write(_error.description);
                return;
            }
        }
        
        function _close() {
            if (_opened) {
                _connection.Close();
                _opened = false;
            }
        }

        function execute(sql) {
            _sql = sql || '';
        
            if (debug) {
                Response.Write(_sql);
                return;
            }
        
            var _result;
            try {
                _open();
            
                if (_sql.substring(0, 6) == 'insert') {
                    _recordset.Open(_sql, _connection, 1, 1);
                    _recordset.Open('select @@IDENTITY as LastId', _connection, 1, 1);
                    _result = _recordset(0).Value;
                    _recordset.Close();
                }
                else {
                    _result = _connection.Execute(_sql);
                }
            }
            catch (_error) {
                Response.Write(_error.Description);
                return;
            }
            finally {
                _close();
            }
            
            return _result;
        }
        
        function query(field, table, condition, order, number, page, size, relative) {
            var _field = field || '';
            var _table = table || '';
            var _condition = condition || '';
            var _order = order || '';
            
            var _number = number || 0;
            var _page = page || 0;
            var _size = size || 0;
            
            var _relative = relative || false;
            
            var _sql = '';
            
            if (_field.substring(0, 6) == 'select') {
                _sql = _field;
            }
            else {
                _sql = 'select ';
                if (typeof(_number) == 'number' && _number != 0) {
                    _sql += 'top ' + _number + ' ';
                }
                _sql += _field + ' from ' + _table;
                if (_condition) {
                    _sql += ' where ' + _condition;
                }
                if (_order) {
                    _sql += ' order by ' + _order;
                }
            }
            
            if (debug) {
                Response.Write(_sql);
                return;
            }
            
            var _result = [];
            try {
                _open();
                _recordset.Open(_sql, _connection, 1, 1);
            
                if ((_size > 0) && (_page > 0) && (_recordset.RecordCount > 0)) {
                    _recordset.PageSize = _size;
                    _recordset.AbsolutePage = _page > _recordset.PageCount ? _recordset.PageCount : _page;
                    _recordCount = _recordset.RecordCount > _size * _page ? _size : _recordset.RecordCount - (_page - 1) * _size ;
                }
                else {
                    _recordCount = _recordset.RecordCount;
                }
                pageCount = _recordset.PageCount;
            
                if (_recordCount > 0) {
                    for (var _index = 0; _index < _recordCount; _index++) {
                        _result[_index] = {};
                        for (var _fieldIndex = 0; _fieldIndex < _recordset.Fields.Count; _fieldIndex++) {
                            _result[_index][_fieldIndex] = _recordset(_fieldIndex).Value;
                            _result[_index][_recordset.fields(_fieldIndex).name] = _recordset(_fieldIndex).Value;
                            _result[_index][_recordset.fields(_fieldIndex).name.substring(0, 1).toLowerCase() + _recordset.Fields(_fieldIndex).Name.substring(1)] = _recordset(_fieldIndex).Value;
                        }
                        _recordset.MoveNext();
                    }
                }
                _recordset.Close();    
            }
            catch (_error) {
                Response.Write(_error.Description);
                return;
            }
            finally {
                _close();
            }
            
            if (_relative) {
                for (var _index = 0; _index < _result.length; _index++) {
                    for (var _property in _result[_index]) {
                        if (_property.length > 2 && _property.substring(_property.length - 2, _property.length) == 'Id') {
                            _result[_index][_property.substring(0, _property.length - 2)] = query('*', '[' + _property.substring(0, _property.length - 2) + ']', '[Id] = ' + _result[_index][_property], '', 1);
                        }
                    }
                }
            }
            if (_number == 1)
                return _result[0];
            else
                return _result;
        }
    <parameter>
    <parameter>
<parameter>
<parameter>
          <parameter>
          <parameter>
      </method>
    </public>
    <implements>
    <script>
    <![CDATA[
        var version = '1.00';
        var connectionString = '';
        var path = '';
        var debug = false;
        var pageCount = 0;
        
        var _connection = Server.CreateObject('ADODB.Connection');
        var _recordset = Server.CreateObject('ADODB.Recordset');
        
        var _opened = false;
        var _recordCount = 0;
        
        function _open() {
            try {
                if (connectionString) {
                    _connection.ConnectionString = connectionString;
                }
                else {
                    _connection.ConnectionString = 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=' + Server.MapPath(path);
                }
                
                _connection.Open();
                _opened = true;
            }
            catch (_error) {
                Response.Write(_error.description);
                return;
            }
        }
        
        function _close() {
            if (_opened) {
                _connection.Close();
                _opened = false;
            }
        }

        function execute(sql) {
            _sql = sql || '';
        
            if (debug) {
                Response.Write(_sql);
                return;
            }
        
            var _result;
            try {
                _open();
            
                if (_sql.substring(0, 6) == 'insert') {
                    _recordset.Open(_sql, _connection, 1, 1);
                    _recordset.Open('select @@IDENTITY as LastId', _connection, 1, 1);
                    _result = _recordset(0).Value;
                    _recordset.Close();
                }
                else {
                    _result = _connection.Execute(_sql);
                }
            }
            catch (_error) {
                Response.Write(_error.Description);
                return;
            }
            finally {
                _close();
            }
            
            return _result;
        }
        
        function query(field, table, condition, order, number, page, size, relative) {
            var _field = field || '';
            var _table = table || '';
            var _condition = condition || '';
            var _order = order || '';
            
            var _number = number || 0;
            var _page = page || 0;
            var _size = size || 0;
            
            var _relative = relative || false;
            
            var _sql = '';
            
            if (_field.substring(0, 6) == 'select') {
                _sql = _field;
            }
            else {
                _sql = 'select ';
                if (typeof(_number) == 'number' && _number != 0) {
                    _sql += 'top ' + _number + ' ';
                }
                _sql += _field + ' from ' + _table;
                if (_condition) {
                    _sql += ' where ' + _condition;
                }
                if (_order) {
                    _sql += ' order by ' + _order;
                }
            }
            
            if (debug) {
                Response.Write(_sql);
                return;
            }
            
            var _result = [];
            try {
                _open();
                _recordset.Open(_sql, _connection, 1, 1);
            
                if ((_size > 0) && (_page > 0) && (_recordset.RecordCount > 0)) {
                    _recordset.PageSize = _size;
                    _recordset.AbsolutePage = _page > _recordset.PageCount ? _recordset.PageCount : _page;
                    _recordCount = _recordset.RecordCount > _size * _page ? _size : _recordset.RecordCount - (_page - 1) * _size ;
                }
                else {
                    _recordCount = _recordset.RecordCount;
                }
                pageCount = _recordset.PageCount;
            
                if (_recordCount > 0) {
                    for (var _index = 0; _index < _recordCount; _index++) {
                        _result[_index] = {};
                        for (var _fieldIndex = 0; _fieldIndex < _recordset.Fields.Count; _fieldIndex++) {
                            _result[_index][_fieldIndex] = _recordset(_fieldIndex).Value;
                            _result[_index][_recordset.fields(_fieldIndex).name] = _recordset(_fieldIndex).Value;
                            _result[_index][_recordset.fields(_fieldIndex).name.substring(0, 1).toLowerCase() + _recordset.Fields(_fieldIndex).Name.substring(1)] = _recordset(_fieldIndex).Value;
                        }
                        _recordset.MoveNext();
                    }
                }
                _recordset.Close();    
            }
            catch (_error) {
                Response.Write(_error.Description);
                return;
            }
            finally {
                _close();
            }
            
            if (_relative) {
                for (var _index = 0; _index < _result.length; _index++) {
                    for (var _property in _result[_index]) {
                        if (_property.length > 2 && _property.substring(_property.length - 2, _property.length) == 'Id') {
                            _result[_index][_property.substring(0, _property.length - 2)] = query('*', '[' + _property.substring(0, _property.length - 2) + ']', '[Id] = ' + _result[_index][_property], '', 1);
                        }
                    }
                }
            }
            if (_number == 1)
                return _result[0];
            else
                return _result;
        }
    ]]>
    </script>
</component>